package cn.iocoder.yudao.module.system.service.appversion;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.appversion.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.appversion.AppVersionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.appversion.AppVersionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * App版本控制 Service 实现类
 *
 * @author 韩艳阳
 */
@Service
@Validated
public class AppVersionServiceImpl implements AppVersionService {

    @Resource
    private AppVersionMapper appVersionMapper;

    @Override
    @Transactional
    public Long createAppVersion(AppVersionSaveReqVO createReqVO) {
        String platform = createReqVO.getPlatform();
        AppVersionDO one = getOne(platform);
        //版本号自增
        if(one != null){
            createReqVO.setVersionCode(one.getVersionCode()+1);
        }

        // 插入
        AppVersionDO appVersion = BeanUtils.toBean(createReqVO, AppVersionDO.class);
        appVersionMapper.insert(appVersion);
        // 返回
        return appVersion.getId();
    }

    @Override
    public void updateAppVersion(AppVersionSaveReqVO updateReqVO) {
        // 校验存在
        validateAppVersionExists(updateReqVO.getId());
        // 更新
        AppVersionDO updateObj = BeanUtils.toBean(updateReqVO, AppVersionDO.class);
        appVersionMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppVersion(Long id) {
        // 校验存在
        validateAppVersionExists(id);
        // 删除
        appVersionMapper.deleteById(id);
    }

    private void validateAppVersionExists(Long id) {
        if (appVersionMapper.selectById(id) == null) {
            throw exception(APP_VERSION_NOT_EXISTS);
        }
    }

    @Override
    public AppVersionDO getAppVersion(Long id) {
        return appVersionMapper.selectById(id);
    }

    @Override
    public AppVersionDO getOne(String platform) {
        return appVersionMapper.selectOne(new LambdaQueryWrapper<AppVersionDO>()
                .eq(AppVersionDO::getPlatform,platform)
                .orderByDesc(AppVersionDO::getVersionCode)
                .last("limit 1"));
    }

    @Override
    public List<AppVersionDO> getAppVersionList(AppVersionPageReqVO pageReqVO) {
        return appVersionMapper.selectLatestVersionsPerPlatform();
    }

    @Override
    public PageResult<AppVersionDO> getAppVersionPage(AppVersionPageReqVO pageReqVO) {
        return appVersionMapper.selectPage(pageReqVO);
    }

}